package admin

import (
	"context"
	"errors"
	"fmt"
	"server/internal/consts"
	"server/internal/service"

	"server/api/admin/system"
)

func (c *ControllerSystem) Websocket(ctx context.Context, req *system.WebsocketReq) (res *system.WebsocketRes, err error) {
	res = new(system.WebsocketRes)
	userInfo, err := service.SystemAuthorityCache().GetUserCacheAuthData(ctx)
	if err != nil {
		return
	}
	if userInfo.Id == 0 {
		err = errors.New("用户未登录")
		return
	}

	host, _ := service.SystemConfig().GetContentByCode(ctx, consts.ConfigCodeWsConnectionHost)
	token, err := service.Jwt().GenerateToken(ctx, userInfo.Id, userInfo.Name, consts.JwtAudienceAdminWs)
	res.URL = fmt.Sprintf("%s?token=%s", host, token)
	return
}
